#import "XMLReader.h"

@implementation XMLReader

@synthesize userList;
@synthesize pid;
@synthesize elementContent;
@synthesize currentId;
@synthesize serverError;
@synthesize vcard;

- (void)dealloc {
	NSLog(@"XMLReader dealloc()");
	[super dealloc];
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
	//TODO this needs work.
	[self.userList release];
    self.userList = [[NSMutableDictionary alloc] initWithCapacity:1];
	[self.userList retain];
}

- (void)parseXML:(NSString *)xml parseError:(NSError **)error
{	
	NSData *xmlData = [xml dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    
    [parser parse];
    
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
    }
    
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    }
    
    if ([elementName isEqualToString:@"PersonalID"]) {
		self.elementContent = [NSMutableString string];
    }
	else if ([elementName isEqualToString:@"User"])
	{
		self.elementContent = [NSMutableString string];
		self.currentId = [attributeDict valueForKey:@"id"];
	}
	else if ([elementName isEqualToString:@"Error"])
	{
		self.elementContent = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"Vcard"])
	{
		self.elementContent = [NSMutableString string];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     
    if (qName) {
        elementName = qName;
    }
    
    if ([elementName isEqualToString:@"PersonalID"]) {
        self.pid = self.elementContent;
        
    } else if ([elementName isEqualToString:@"User"]) {
        [self.userList setObject:self.currentId forKey:elementContent];
		NSLog(@"Added new User to userlist withUsername: %@ and uid: %@", elementContent, currentId);
    } 
	else if ([elementName isEqualToString:@"Error"])
	{
		self.serverError = self.elementContent;
	}
	else if ([elementName isEqualToString:@"Vcard"])
	{
		self.vcard = self.elementContent;
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.elementContent) {
        // If the current element is one whose content we care about, append 'string'
        // to the property that holds the content of the current element.
        [self.elementContent appendString:string];
    }
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError 
														 *)parseError
{
    NSLog(@"Error %i, Description: %@, Line: %i, Column: %i", 
		  [parseError code], [[parser parserError] localizedDescription], 
		  [parser lineNumber], [parser columnNumber]);    
}

@end
